/**
 * Created by yangfan on 2016/8/29.
 */
define(["require",'config/appregister',"i18n","business/news/services/httpServices", 'business/services/moreHttpServices',"business/services/filter",
"business/services/rightComponents","business/home/directives/share",],function(require,app,i18n,httpService,moreHttpServices){
    app.controller('newsDetailCtrl',function($scope,$stateParams,$state,auth,$sce,$rootScope){
        $scope.$parent.isDetailPage = true;
        $scope.$parent.isMainpage = false;
        $scope.name ="新闻详情页" ;
        $scope.draw = { userPngSrc:"them/images/123.jpg"};
        $scope.i18n = i18n;

        if( !angular.isUndefined($stateParams.newsId)){
            $scope.newsId = $stateParams.newsId;
        }
        $scope.breadPath = ["新闻资讯","地铁资讯"];

        $scope.hideAngle = function(index){
            if(index == $scope.breadPath.length-1){
                return true
            }else{
                return false
            }
        };
        $scope.setStyle = function(index){
            return {"border":"1px solid"+"red"}
        };

        $scope.goToDetail = function(state,params){
            //    $state.go(state,params,{reload:true});
            $state.transitionTo(state,params, {
                reload: true, inherit: true, notify: true
            });
        };
		$scope.share = {
    		url:"www.baidu.com",
    		title:'工程头条',
    		summary:''
    	}
        //用户评论页面渲染数据
        function getNews (){
            var req = {
                type:"GET",
                uri:"get",
                data:{
                    newsId :$scope.newsId,
                }
            };
            httpService.newsHttpServices(req).success(function(response){
                if(!response.data) return;

                if(response.data.new.flag){
                    $scope.collectionTitle = "已收藏";
                    $scope.isCollected = true;
                }else{
                    $scope.collectionTitle = "收藏";
                    $scope.isCollected = false;
                }

                $scope.$apply(function(){
                    $scope.news = response.data;
                    $scope.newsContent = $sce.trustAsHtml(response.data.new.content);
                    //$scope.newsContent = response.data.new.content;
                    if($scope.news.category){
                        var typeId = $scope.news.category.scId;
                        getNewsRelevant(typeId);
                    }
                    
                })
            });
        };
        function getLike(){
            var req = {
                type:"GET",
                uri:"getLike",
                data:{
                    newsId :$scope.newsId,
                    count:6
                }
            };
            httpService.newsHttpServices(req).success(function(response){
                if(!response.data) return;
                $scope.$apply(function(){
                    $scope.getLike = response.data;
                    if($scope.getLike.length >5){
                        $scope.getLike = $scope.getLike.splice(0,5);
                    }
                })
            });
        };

        //热门排行
        function getHotRank(){
            var req = {
                type:"GET",
                uri:"getHotRank",
                data:{
                    count:10
                }
            };
            httpService.newsHttpServices(req).success(function(response){
                if(!response.data) return;
                $scope.$apply(function(){
                    $scope.getHotRank = response.data;
                    getDownLoadRankConf($scope.getHotRank);
                })
            });
        };

        function getDownLoadRankConf(data){
            $scope.setConf = {
                data:data,
                idType:"newsId",
                state:"eng.newsDetail",
                title:'头条排行',
                icon:true
            }
        }

        //相关资讯分类
        function getNewsRelevantType(){
            var req = {
                type:"GET",
                uri:"getRelevantType",
                data:{
                    newsId:$scope.newsId,
                    count:10
                }
            };
            httpService.newsHttpServices(req).success(function(response){
                if(!response.data) return;
                $scope.$apply(function(){
                    $scope.RelevantType = response.data;
                })
            });
        };

        //相关推荐
        function getNewsRelevant(typeId){
            var req = {
                type:"GET",
                uri:"getRelevant",
                data:{
                    newsId:$scope.newsId,
                    typeId:typeId,
                    count:6
                }
            };
            httpService.newsHttpServices(req).success(function(response){
                if(!response.data) return;
                $scope.$apply(function(){
                    $scope.getRelevant = response.data;
                })
            });
        };

        getNews();
        getLike();
        getHotRank();
        getNewsRelevantType();

        //更新收藏
        function updateCollection(){
            moreHttpServices.updateCollection($scope.newsId,6).success(function(response){
                if(response.code == 0){
                    $scope.$apply(function(){
                        $scope.collectionTitle = "已收藏";
                        $scope.isCollected = true;
                    })
                }
                if(response.code == 112){
                    $scope.$apply(function(){
                        $scope.collectionTitle = "收藏";
                        $scope.isCollected = false;
                    })
                }
            }).error(function(){

            })
        }



        $scope.isCollected = false;
        $scope.collectionTitle = "收藏";
        $scope.updateCollection = function(){
            auth.isLogin().then(function(){
                updateCollection();
            })
        }
        $scope.goto =function(id){
            $("html,body").animate({scrollTop: $("#"+id)[0].offsetTop - 135}, 500);
        };

        $scope.commentConf = {
            showStar: false,
            conf: {
                id: $stateParams.newsId,
                type: 6
            }
        };

        $scope.otherNews = function(news){
        	$state.go('eng.newsDetail',{'newsId':news});
        }
        $scope.goTo = function(state){
            $state.go(state) ;
        }
        $scope.jumpTo = function(item){
                $state.go("eng.newsDetail",{newsId:item.newsId});
        }

        $scope.goTo1 = function(id){
            sessionStorage.newsList = id;
            $state.go('eng.newsList')
        }
        //标签跳搜索
        $scope.jumpToSearch = function(value){
            $rootScope.GLOBALKEYWORD =  value;
            sessionStorage.searchKeyWord = value;
            $state.transitionTo("search.news", {}, {
                reload: true, inherit: false, notify: true
            });
        }
    })
});

